package com.xcfw.dorm.controller.dormcenter;

import com.xcfw.dorm.controller.BaseController;
import com.xcfw.dorm.entity.Assistant;
import com.xcfw.dorm.entity.NumEnum;
import com.xcfw.dorm.entity.ReturnJson;
import com.xcfw.dorm.exception.ServiceException;
import com.xcfw.dorm.exception.ValidationException;
import com.xcfw.dorm.sevice.ZSGCService;
import com.xcfw.dorm.seviceimpl.dormcenter.AssistantServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

/**
 * @program: Dorm
 * @description: 辅导员
 * @author: Bruse Queen
 * @create: 2018-04-07 16:41
 **/
@RestController
@Slf4j
@RequestMapping(value = "/dormcenter")
@CrossOrigin
public class AssistantController extends BaseController {
    @Autowired
    @Qualifier("AssistantServiceImpl")
    private ZSGCService zsgcService;

    @RequestMapping(value = "/addAssistant", method = RequestMethod.POST)
    public ReturnJson addAssistant(@RequestBody Assistant assistant) {
        try {
            zsgcService.add(assistant);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "辅导员信息新增成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("AssistantController---addAssistant--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/deleteAssistant", method = RequestMethod.POST)
    public ReturnJson deleteAssistant(@RequestBody Assistant assistant) {
        try {
            zsgcService.delete(assistant.getId());
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "辅导员信息删除成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("AssistantController---deleteAssistant--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/updateAssistant", method = RequestMethod.POST)
    public ReturnJson updateAssistant(@RequestBody Assistant assistant) {
        try {
            zsgcService.update(assistant);
            result = new ReturnJson(NumEnum.SUCCESS.getIndex(), "辅导员信息修改成功！");
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("AssistantController---updateAssistant--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }

    @RequestMapping(value = "/selectAssistant", method = RequestMethod.POST)
    public ReturnJson selectAssistant(@RequestBody Assistant assistant) {
        try {

            result = (ReturnJson) zsgcService.select(assistant);
        } catch (ValidationException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (ServiceException e) {
            result = new ReturnJson(NumEnum.FAIL.getIndex(), e.getMessage());
        } catch (Exception e) {
            log.error("AssistantController---selectAssistant--error", e);
            result = new ReturnJson(NumEnum.FAIL.getIndex(), "数据异常,请稍后再试！");
        }
        return result;
    }
}
